package com.virtualtrainer_01.dao;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.text.format.Time;

import com.virtualtrainer_01.model.Exercise;
import com.virtualtrainer_01.model.Group;
import com.virtualtrainer_01.model.Model;
import com.virtualtrainer_01.model.Training;
import com.virtualtrainer_01.util.NameList;

public class TrainingDB extends SQLiteDB {

	public TrainingDB(Context c) {
		super(c, DATABASE_TABLE);
	}

	public static final String KEY_ID = "_id";
	public static final String KEY_TIME = "time";
	public static final String KEY_DURATION = "duration";
	public static final String DATABASE_TABLE = "training";
	public static final String KEY_NAME = "name";

	@Override
	public String getData() {
		String[] columns = new String[] { KEY_ID, KEY_TIME, KEY_DURATION };
		Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
				null, null);
		String result = "";

		int iRow = c.getColumnIndex(KEY_ID);
		int iTime = c.getColumnIndex(KEY_TIME);
		int iDur = c.getColumnIndex(KEY_DURATION);

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
			result = result + c.getString(iRow) + " " + c.getString(iTime)
					+ " " + c.getString(iDur) + " ";
		return result;
	}

	@Override
	public long createEntry(Model entry) {
		Training t = (Training) entry;
		ContentValues cv = new ContentValues();
		cv.put(KEY_TIME,
				t.getBeginTime().monthDay + "-" + t.getBeginTime().month + "-"
						+ t.getBeginTime().year + "&" + t.getBeginTime().second+"/"
						+ t.getBeginTime().minute + "/" + t.getBeginTime().hour);
		cv.put(KEY_DURATION, t.getDuration());
		cv.put(KEY_NAME, t.getName());
		return ourDatabase.insert(DATABASE_TABLE, null, cv);
	}

	public Training getTraining(int id) {
		String sql = "SELECT * FROM " + DATABASE_TABLE + " t where t." + KEY_ID
				+ "=" + "'" + id + "';";
		Cursor c = ourDatabase.rawQuery(sql, null);

		int iName = c.getColumnIndex(KEY_NAME);
		int iDuration = c.getColumnIndex(KEY_DURATION);
		int iTime = c.getColumnIndex(KEY_TIME);
		int iRow = c.getColumnIndex(KEY_ID);

		Training tr = new Training();
		TrainingGroupDB tgdb = new TrainingGroupDB(ourContext);
		TrainingExerciseDB tedb = new TrainingExerciseDB(ourContext);
		
		ExerciseDB edb = new ExerciseDB(ourContext);
		GroupDB gdb = new GroupDB(ourContext);
		tgdb.open();
		tedb.open();
		edb.open();
		gdb.open();
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			tr.setName(c.getString(iName));
			tr.setDuration(Integer.parseInt(c.getString(iDuration)));
			Time t = new Time();
			String time = c.getString(iTime);
			String times[] = time.split("&");
			String data[] = times[0].split("-");
			String ora[] = times[1].split("/");
			t.set(Integer.parseInt(data[0]), Integer.parseInt(data[1]),
					Integer.parseInt(data[2]), Integer.parseInt(ora[0]),
					Integer.parseInt(ora[1]), Integer.parseInt(ora[2]));
			tr.setBeginTime(t);
			
			List<Integer> idExList = tedb.getIdExerciseList(Integer.parseInt(c.getString(iRow)));
			NameList<Exercise> exList =  new NameList<Exercise>();
			for(int i = 0; i< idExList.size();i++)
				exList.add(edb.getExercise(idExList.get(i)));			
			tr.setExerciseList(exList);
			
			List<Integer> idGrList = tgdb.getIdGroupList(Integer.parseInt(c.getString(iRow)));
			NameList<Group> grList =  new NameList<Group>();
			for(int i = 0; i< idGrList.size();i++)
				grList.add(gdb.getGroup(idGrList.get(i)));			
			tr.setGroupList(grList);
		}
		tgdb.close();
		tedb.close();
		edb.close();
		gdb.close();
		return tr;
	}
}
